#include "System.h"
#include "Arduino.h"
#include "HardwareSerial.h"
#include <windows.h>

static const int PINS_COUNT = 16;

const long long startTS = current_timestamp();

uint8_t modes[PINS_COUNT];
uint8_t dws[PINS_COUNT];
int aws[PINS_COUNT];

unsigned long millis(void) {
    return (unsigned long) (current_timestamp() - startTS);
};

void delay(unsigned long i) {
    Sleep(i);
}

void pinMode(uint8_t t, uint8_t uint8T) {
    if (modes[t] != uint8T) {
#ifdef ENABLE_WIRES_DEBUG
        Serial.print("PIN: ");
        Serial.print(t);
        Serial.print("-->");
        Serial.println((uint8T == INPUT) ? "INPUT" : ((uint8T == OUTPUT) ? "OUTPUT" : ((uint8T == INPUT_PULLUP) ? "INPUT_PULLUP" : "UNKNOWN")));
        #endif
    }
    modes[t] = uint8T;
}

void digitalWrite(uint8_t t, uint8_t uint8T) {
    if (dws[t] != uint8T) {
#ifdef ENABLE_WIRES_DEBUG
        Serial.print("DW: ");
        Serial.print(t);
        Serial.print("-->");
        Serial.println(uint8T);
        #endif
    }
    dws[t] = uint8T;
}

int digitalRead(uint8_t t) {
    return rand() % 2;
}

int analogRead(uint8_t t) {
    return 0;
}

void analogWrite(uint8_t t, int i) {
    if (aws[t] != i) {
#ifdef ENABLE_WIRES_DEBUG
        Serial.print("AW: ");
        Serial.print(t);
        Serial.print("-->");
        Serial.print(i);
        Serial.println();
        #endif
    }
    aws[t] = i;
}

long map(long x, long in_min, long in_max, long out_min, long out_max) {
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

class Initializer {
public:
    Initializer() {
        for (int i = 0; i < PINS_COUNT; ++i) {
            modes[i] = 255;
            dws[i] = 255;
            aws[i] = -1;
        }
    }
};

Initializer initializer;


